﻿using STMS.BLL;
using STMS.Models.DModels;
using STMS.STMSapp.FModels;
using STMS.STMSapp.product;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.STMSapp.store
{
    public partial class FrmStoreList : Form
    {
        public FrmStoreList()
        {
            InitializeComponent();
        }

        private void FrmStoreList_Load(object sender, EventArgs e)
        {
            tbKeyWords.Clear();
            LoadStoreList();
        }

        StoreBLL storeBLL = new StoreBLL();
        private void LoadStoreList()
        {
            string keywords = tbKeyWords.Text.Trim();
            //chkShowDel.Checked = false;
            bool isDel = chkShowDel.Checked;
            // 如果查询的是已删除的仓库(isDeleted == 1)，就隐藏更新、删除按钮
            dgvStoreList.ShowDgvCols(isDel);
            dgvStoreList.Columns["colAddRegion"].Visible = true;


             List<StoreInfo> storeList = storeBLL.GetStoreInfos(keywords, isDel); // 获取数据操作
   

            if (storeList.Count > 0)
            {
                dgvStoreList.AutoGenerateColumns = false;
                dgvStoreList.DataSource = storeList;
                SetEnableButtons(true);

            }
            else
            {
                dgvStoreList.DataSource = null;
                SetEnableButtons(false);

            }
        }
        private void SetEnableButtons(bool enable)
        {
            btnDelStore.Enabled = enable;
            btnSearchStore.Enabled = enable;
        }

        private void chkShowDel_CheckedChanged(object sender, EventArgs e)
        {
            tbKeyWords.Clear();
            LoadStoreList();
        }
        // 搜索
        private void btnSearchStore_Click(object sender, EventArgs e)
        {
            LoadStoreList();
        }

        private void tbKeyWords_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                LoadStoreList();
            }
        }

        // 新增仓库
        private void btnAddStore_Click(object sender, EventArgs e)
        {
            ShowStoreInfoPage(1, 0);

        }

        /// <summary>
        /// 打开仓库信息窗口
        /// </summary>
        /// <param name="actType">新增：1  编辑：2  添加子类：3   详细信息：4</param>
        /// <param name="storeId">仓库编号</param>
        private void ShowStoreInfoPage(int actType, int storeId)
        {

            FInfoData fInfoData = new FInfoData()
            {
                ActType = actType,
                FId = storeId
            };

            FrmStoreInfo frmStoreInfo = new FrmStoreInfo(fInfoData);
            frmStoreInfo.ReLoadStoreList += () => LoadStoreList();

            // 子窗口在屏幕中间设置
            frmStoreInfo.StartPosition = FormStartPosition.CenterScreen;
            frmStoreInfo.ShowDialog();

        }
        /// <summary>
        /// 打开仓库分区页面
        /// </summary>
        /// <param name="storeId"></param>
        private void ShowStoreRegionInfoPage(int storeId)
        {
            FrmStoreRegion frmStoreRegionInfo = new FrmStoreRegion();

            FInfoData fInfoData = new FInfoData()
            {
                ActType = 3,
                FId = storeId
            };

            // 子窗口在屏幕中间设置
            frmStoreRegionInfo.StartPosition = FormStartPosition.CenterScreen;
            frmStoreRegionInfo.ShowDialog();
        }
        private void dgvStoreList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // 当前点击的单元格
            var cell = dgvStoreList.Rows[e.RowIndex].Cells[e.ColumnIndex];
            string headText = cell.FormattedValue.ToString();
            StoreInfo selectStore = dgvStoreList.Rows[e.RowIndex].DataBoundItem as StoreInfo;


            switch (headText)
            {
                case "添加分区":
                    // 打开添加仓库分区信息页面
                    ShowStoreRegionInfoPage(selectStore.StoreId);
                    break;
                case "修改":
                    // 打开仓库信息页面
                    ShowStoreInfoPage(2, selectStore.StoreId);
                    break;
                case "删除":
                    // 执行逻辑删除(IsDeleted == 1) 
                    if (MessageBox.Show($"确定删除仓库:{selectStore.StoreName}?", "删除仓库", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        DeleteStore(selectStore, 0);
                        LoadStoreList();
                        break;
                    }
                    break;
                case "恢复":
                   
                    if (MessageBox.Show($"确定恢复仓库:{selectStore.StoreName}?", "恢复仓库", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        DeleteStore(selectStore,2);
                        LoadStoreList();
                        break;
                    }
                    break;

                    break;
                case "移除":
                    // 真删除 delete相关数据
                    break;


            }
        }/// <summary>
        /// 删除仓库逻辑
        /// </summary>
        /// <param name="storeInfo"></param>
        /// <param name="delCode">0为假删除，1为真删除, 2为恢复仓库</param>
        private void DeleteStore(StoreInfo storeInfo, int delCode)
        {
            bool HasRegion = storeBLL.HasRegion(storeInfo.StoreId);
            switch (delCode)
            {
                // 假删除
                case 0:
                    int delRes = storeBLL.LogicDelStore(storeInfo.StoreId);
                    if(delRes == 0)
                    {
                        MessageBox.Show("删除仓库失败！");
                        return;
                    }
                    else if(delRes == 2)
                    {
                        MessageBox.Show("仓库包含分区，无法删除！");
                    }
                    else if(delRes == 1)
                    {
                        MessageBox.Show("删除成功！");
                    }

                    break;
                case 1:
                    // 真删除逻辑
                    break;
                case 2:
                    // 恢复逻辑
                    if (storeBLL.RecoverStore(storeInfo.StoreId))
                    {
                        MessageBox.Show("仓库恢复成功！");
                           return;
                    }
                    else
                    {
                        MessageBox.Show("仓库恢复失败！");
                    }
                    break;
                    

            }
        }


    }
}
